package org.adobe.utils {
	import flash.utils.ByteArray;

	/**
	 * 字符串转换类
	 * 对中文字符无法转换
	 */	
	public class StrBytes {
		
		public static function toString(ba:ByteArray):String {
			var acum:String = "";
			ba.position = 0;
			while (ba.position < ba.length){
				var dat:String = ba.readUnsignedByte().toString(16); //取无符号的字节
				//trace(dat);
				while (dat.length < 2)
					dat = "0" + dat;
				acum += dat;
			}
			ba.position = 0;
			return acum;
		}
		
		/**
		 * 二进制加密的字符串
		 * 每两个字符组合成一个16进制的值 
		 * 将组合的int值写入字节数组
		 */
		public static function toByteArray(str:String):ByteArray {
			if (str.length % 2 != 0) return null;
			var ba:ByteArray = new ByteArray(); 
			for (var i:int = 0; i < str.length; i += 2){
				var num:int = parseInt("0x" + str.substr(i, 2)); //'0x' + str.charAt(i) + str.charAt(i + 1)
				ba.writeByte(num); //写入一个int字节 (int---2位)
			}
			ba.position = 0;
			return ba;
		}
		/*
		function toByteArray(a:Array):ByteArray {
		var bytes:ByteArray = new ByteArray();
		for each (var b:int in a) {
		bytes.writeByte(b);
		}
		return bytes;
		}
		var testData:ByteArray = toByteArray([
		0x53, 0x75, 0x63, 0x63, 0x65, 0x73,
		0x73, 0x3a, 0x20, 0x53, 0x74, 0x72,
		0x69, 0x6e, 0x67, 0x20, 0x72, 0x65,
		0x2d, 0x65, 0x6e, 0x63, 0x6f, 0x64,
		0x65, 0x64, 0x2e //, ...
		]);
		for(var i:int=0; i<testData.length; i++){
		trace(testData[i],String.fromCharCode(testData[i]));
		}		
		*/
	}
}